<?php

class my_model extends jqxGrid_CI_Model {

    protected $mytable,$colID;
    function __construct($mytable="")
    {
        parent::__construct();
        $this->mytable=$mytable;
        $this->colID="ID";
        if($mytable=="dtb_product")
            $this->colID="ProductID"; 
        if($mytable=="dtb_orders")
            $this->colID="OrderID"; 
        if($mytable=="_type")
            $this->colID="_id"; 
    }
    function toString(){
        echo $this->mytable;
    }
    function get($ID=""){
        $where=array($this->colID=>$ID);
        $query=$this->db->get_where($this->mytable, $where); 
        return $query->result();
    }
    function update($ID,$Params){
        $this->db->set('_update', "NOW()",false)
            ->where($this->colID, $ID)
            ->update($this->mytable, $Params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function updateColumn($ID,$col,$val){
        $Params[$col]=$val;
        $this->db->set('Update', "NOW()",false)
            ->where($this->colID, $ID)
            ->update($this->mytable, $Params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function insert($Params){
        $this->db->set('Insert', "NOW()",false)
            ->insert($this->mytable, $Params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function delete($ID){
        $this->db->set('Delete', "NOW()",false);
        $this->db->where($this->colID, $ID);
        $this->db->update($this->mytable); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        echo $this->db->last_query();
        if($count==1) return true;
        return false;
    }
    function restore($ID){
        $this->db->set('Delete', "NULL",false);
        $this->db->where($this->colID, $ID);
        $this->db->update($this->mytable); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function deleteInList($ID){
        $this->db->set('Delete', "NOW()",false);
        $this->db->where_in($this->colID, $ID);
        $this->db->update($this->mytable); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count>0) return true;
        return false;
    }
    function restoreInList($ID){
        $this->db->set('Delete', "NULL",false);
        $this->db->where_in($this->colID, $ID);
        $this->db->update($this->mytable); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count>0) return true;
        return false;
    }
    function updateInList($ID,$Params){
        $this->db->set('Update', "NOW()",false)
            ->where_in($this->colID, $ID)
            ->update($this->mytable, $Params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count>0) return true;
        return false;
    }
    function updateColumnInList($ID,$col,$val){
        $Params[$col]=$val;
        $this->db->set('Update', "NOW()",false)
            ->where_in($this->colID, $ID)
            ->update($this->mytable, $Params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count>0) return true;
        return false;
    }
    function binding($usinglimit=true){
        $strQuery="
            SELECT SQL_CALC_FOUND_ROWS `$this->mytable`.*
            FROM `$this->mytable`
            ";
        $strWhere="
            WHERE  true
            ";
        if($_SESSION["ADP"][CCTRL]["display"]==0){
            $strWhere.=" AND `Delete` IS NULL";
        }elseif($_SESSION["ADP"][CCTRL]["display"]==-1){
            $strWhere.=" AND `Delete` IS NOT NULL";
        }
        $strOrderBy="Order By `Insert` DESC ";
        $strGroupBy="";
        $config=array(
            "strQuery"=>$strQuery,
            "strWhere"=>$strWhere,
            "strOrderBy"=>$strOrderBy,
            "strGroupBy"=>$strGroupBy,
            "usingLimit"=>$usinglimit,
            "fields"=>array(),
            "datefields"=>array()
        );
        $this->init($config);
        return $this->jqxBinding();
    }
}
?>
